﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Commons
{
    public class NodeCollection:IList<Node>
    {
        private List<Node> _elements;
        public bool IsReadOnly
        {
            get { return false; }
        }
        public int Count
        {
            get { return this._elements.Count; }
        }
        public Node this[int index]
        {
            get { return this._elements[index]; }
            set { this._elements[index] = value; }
        }
        public Node this[string id]
        {
            get { return this._elements.Find(delegate(Node nd) { return nd.ID == id; }); }
            set { this[id] = value; }
        }
        public NodeCollection()
        {
            this._elements = new List<Node>();
        }
        public Node Find(string Id)
        {
            IEnumerable<Node> nodes = this.Select<Node, Node>(
                delegate(Node nd)
                {
                    if (nd.ID == Id)
                        return nd;
                    else return null;
                }
                );
            foreach (Node item in nodes)
            {
                return item;
            }
            return null;
        }
        public IEnumerator<Node> GetEnumerator()
        {
            return this._elements.GetEnumerator();
        }
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this._elements.GetEnumerator();
        }
        public bool Remove(Node item)
        {
            return this._elements.Remove(item);
        }
        public void CopyTo(Node[] array, int arrayIndex)
        {
            this._elements.CopyTo(array, arrayIndex);
        }
        public bool Contains(Node item)
        {
            return this._elements.Contains(item);
        }
        public void Clear()
        {
            while (this.Count > 0)
            {
                Node element = this[0];
                this.Remove(element);
            }
        }
        public void Add(Node item)
        {
            this._elements.Add(item);
        }
        public void RemoveAt(int index)
        {
            Node element = this[index];

            if (element != null)
            {
                this.Remove(element);
            }
        }
        public void Insert(int index, Node item)
        {
            this._elements.Insert(index, item);
        }
        public int IndexOf(Node item)
        {
            return this._elements.IndexOf(item);
        }
    }
}
